package com.jh.user.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.base.MoreObjects;
import com.jh.user.pojo.enums.ChannelMerType;
import com.jh.user.pojo.enums.PayType;
import com.jh.user.pojo.enums.UserGrade;
import com.jh.user.service.BrandRateRequest;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**平台从渠道拿到的结算费率*/
@Entity
@Table(name="t_brand_rate")
public class BrandRate implements Serializable{

	private static final long serialVersionUID = 1L;

	public static BrandRate init(BrandRateRequest rateRequest) {
        BrandRate brandRate = new BrandRate();
        brandRate.setBrandId(rateRequest.getBrandId());
        brandRate.setChannelId(rateRequest.getChannelId());
        brandRate.setUserGrade(rateRequest.getUserGrade());
        brandRate.setPayType(rateRequest.getType());
        brandRate.setStatus("1");
        Date now = new Date();
        brandRate.setUpdateTime(now);
        brandRate.setCreateTime(now);
		brandRate.setChannelMerType(ChannelMerType.of(rateRequest.getMerType()).getDesc());
        return brandRate;
    }

    public static BrandRate createOne(BrandRateRequest brandRateRequest) {
		BrandRate brandRate = new BrandRate();
		if (null != brandRateRequest.getBrandId()) {
			brandRate.setBrandId(brandRateRequest.getBrandId());
		}
		if (null != brandRateRequest.getChannelId()) {
			brandRate.setChannelId(brandRateRequest.getChannelId());
		}
		if (null != brandRateRequest.getUserGrade()) {
			brandRate.setUserGrade(brandRateRequest.getUserGrade());
		}
		if (null != brandRateRequest.getRate()) {
			brandRate.setRate(brandRateRequest.getRate());
		}
		if (null != brandRateRequest.getMinRate()) {
			brandRate.setMinrate(brandRateRequest.getMinRate());
		}
		if (null != brandRateRequest.getExtraFee()) {
			brandRate.setExtraFee(brandRateRequest.getExtraFee());
		}
		if (null != brandRateRequest.getGradeRate()) {
			brandRate.setGradeRate(brandRateRequest.getGradeRate());
		}
		brandRate.setStatus("1");
		if (null != brandRateRequest.getType()) {
			brandRate.setPayType(brandRateRequest.getType());
		}
		if (null != brandRateRequest.getMerType()) {
			brandRate.setChannelMerType(brandRateRequest.getMerType());
		}
		brandRate.setUpdateTime(Calendar.getInstance().getTime());
		brandRate.setCreateTime(Calendar.getInstance().getTime());
		if (null != brandRateRequest.getMerType()) {
			brandRate.setChannelMerType(brandRateRequest.getMerType());
		}
		return brandRate;
	}

    public void supply(BrandRateRequest rateRequest) {
	    if (rateRequest.getRate() != null) {
	        setRate(rateRequest.getRate());
        }
        if (rateRequest.getMinRate() != null) {
            setMinrate(rateRequest.getMinRate());
        }
        if (rateRequest.getExtraFee() != null) {
            setExtraFee(rateRequest.getExtraFee());
        }
        if (rateRequest.getGradeRate() != null) {
            setGradeRate(rateRequest.getGradeRate());
        }
        setUpdateTime(new Date());
    }

	@Id
	@Column(name="id")
	private long id;

	@Column(name="brand_id")
	private long brandId;

	/**
	 * 通道的Id,1:畅捷商旅，2：畅捷101，3：畅捷102
	 */
	@Column(name="channel_id")
	private long channelId;

	/**
	 * 用户角色，0：成员工，1：主管，2：经理，3：合伙人
	 */
	@Enumerated(value = EnumType.ORDINAL)
	@Column(name="user_grade")
	private UserGrade userGrade;

	/**通道的结算费率  普通用户*/
	@Column(name="rate")
	private BigDecimal rate;

	/**上游给的渠道的最低费率*/
	@Column(name="minrate")
	private BigDecimal minrate;

	/**手续费*/
	@Column(name="extra_fee")
	private BigDecimal extraFee;

	/** 推荐人分润费率 */
	@Column(name = "grade_rate")
	private BigDecimal gradeRate;

	/**
	 * 贴牌上费率
	 */
	@Column(name = "brand_rate")
	private BigDecimal brandRate;

	/***状态*/
	@Column(name="status")
	private String status;

	/**
	 * 交易类型，1：交易，2：代还
	 */
	@Column(name="pay_type")
	private Short payType;

	@Column(name = "update_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	private Date updateTime;

	/**创建时间*/
	@Column(name="create_time")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	/**通道商户类型 1落地商户，2线上商户*/
	@Column(name = "channel_mer_type")
	private String channelMerType;


	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getBrandId() {
		return brandId;
	}

	public void setBrandId(long brandId) {
		this.brandId = brandId;
	}

	public long getChannelId() {
		return channelId;
	}

	public void setChannelId(long channelId) {
		this.channelId = channelId;
	}

	public UserGrade getUserGrade() {
		return userGrade;
	}

	public void setUserGrade(UserGrade userGrade) {
		this.userGrade = userGrade;
	}

	public BigDecimal getRate() {
		return rate;
	}

	public void setRate(BigDecimal rate) {
		this.rate = rate;
	}

	public BigDecimal getMinrate() {
		return minrate;
	}

	public void setMinrate(BigDecimal minrate) {
		this.minrate = minrate;
	}

	public BigDecimal getExtraFee() {
		return extraFee;
	}

	public void setExtraFee(BigDecimal extraFee) {
		this.extraFee = extraFee;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Short getPayType() {
		return payType;
	}

	public void setPayType(Short payType) {
		this.payType = payType;
	}

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public PayType ofPayType() {
		return PayType.of(getPayType());
	}

	public BigDecimal getGradeRate() {
		return gradeRate;
	}

	public void setGradeRate(BigDecimal gradeRate) {
		this.gradeRate = gradeRate;
	}

	public BigDecimal getBrandRate() {
		return brandRate;
	}

	public void setBrandRate(BigDecimal brandRate) {
		this.brandRate = brandRate;
	}

	public String getChannelMerType() {
		return channelMerType;
	}

	public void setChannelMerType(String channelMerType) {
		this.channelMerType = channelMerType;
	}

	public ChannelMerType ofChannelMerType() {
		return ChannelMerType.of(getChannelMerType());
	}
	@Override
	public String toString() {
		return MoreObjects.toStringHelper(this)
				.add("id", id)
				.add("brandId", brandId)
				.add("channelId", channelId)
				.add("userGrade", userGrade)
				.add("rate", rate)
				.add("minrate", minrate)
				.add("extraFee", extraFee)
				.add("gradeRate", gradeRate)
				.add("status", status)
				.add("payType", payType)
				.add("updateTime", updateTime)
				.add("createTime", createTime)
				.add("channelMerType", channelMerType)
				.toString();
	}
}
